
import { BUNDLE_TYPE_ENUM } from "../../base/EnumIndex";
import { ActionTools } from "../../common/ActionTools";
import { Tools } from "../../common/Tools";
import { AudioManager1 } from "../../manager/AudioManager1";

const { ccclass, property } = cc._decorator;

/** 发送卡片节点 */
@ccclass
export default class SendCardItem extends cc.Component {
    @property({ type: cc.Node, displayName: "发送卡片父节点" })
    sendParent: cc.Node = null;
    @property({ type: cc.Node, displayName: "发送到之后父节点" })
    toParent: cc.Node = null;
    @property(cc.Boolean)
    isAni: boolean = false;
    @property({ type: cc.Node, displayName: "合书" })
    shu1: cc.Node = null;
    @property({ type: cc.Node, displayName: "展开书" })
    shu2: cc.Node = null;

    /** 发送位置 */
    @property(cc.Vec2)
    sendPos: cc.Vec2 = null;
    private isClick = true;
    onLoad() {
        this.node.on("click", () => {
            if (!this.isClick) {
                return;
            }
            if (this.sendParent.childrenCount == 0) {
                return;
            }
            AudioManager1.playAudio("tanchu", 1, BUNDLE_TYPE_ENUM.GAME)
            this.isClick = false;
            if (this.isAni) {
                let child = this.node.getChildByName("shu_ske");
                let armature = child.getComponent(dragonBones.ArmatureDisplay)
                armature.playAnimation("dakai_1", 1);
                armature.timeScale = 0.7;

                cc.tween(child).delay(0.14).call(() => {
                    armature.playAnimation("budong", 1);
                    let node = this.sendParent.children[ActionTools._ins.returnRanNum(0, this.sendParent.children.length - 1)];
                    node.active = true;
                    this.sendNode(node);
                }).start();
            } else {
                this.shu2.active = true;
                this.shu1.active = false;
                let node = this.sendParent.children[ActionTools._ins.returnRanNum(0, this.sendParent.children.length - 1)];
                node.active = true;
                this.sendNode(node);
            }
        });
    }
    /** 发送节点 */
    sendNode(node: cc.Node) {
        Tools.setNodeParent(node, this.toParent);
        node.zIndex = 2;
        let offXs = [-400, 400];
        let ran = Tools.random(0, 1)
        let offX = offXs[ran];
        cc.tween(node)
            .to(0.2, { position: cc.v3(this.sendPos.x + offX, this.sendPos.y) })
            .call(() => {
                this.isClick = true;
                if (!this.isAni) {
                    this.shu2.active = false;
                    this.shu1.active = true;
                }
                if (this.sendParent.childrenCount == 0) {
                    this.node.active = false;
                    if (!this.isAni) {
                        this.shu2.active = false;
                        this.shu1.active = false;
                    }

                }
            }).start();
    };
}
